#include <QtCore/QFile>
#include <QtXml/QDomDocument>
#include <QtXml/QDomElement>
#include <QtXml/QDomNode>
#include "QpwDatabase.h"

using namespace QpwXML;

/**************************************************************************************************/

Database::Database( const QString & filePath ) {

    if ( filePath.count() )
        load(filePath);

}

Database::~Database() {
}

bool Database::load( const QString & filePath ) {

    QFile file( filePath.isEmpty() ? p_FilePath : filePath );
    bool ok = true;
    p_FilePath = filePath;

    if ( file.open(QIODevice::ReadOnly) ) {

        QDomDocument domDocument;

        if ( domDocument.setContent(&file) ) {

            QDomElement rootElement = domDocument.documentElement();
            QDomNode currentRow = rootElement.firstChild(),
                currentCell;
            QDomElement rowElement,
                cellElement;
            QList<Values> data;

            while( !currentRow.isNull() ) {

                rowElement = currentRow.toElement();

                if ( !rowElement.isNull() ) {

                    Values values;
                    currentCell = rowElement.firstChild();

                    while ( !currentCell.isNull() ) {

                        cellElement = currentCell.toElement();

                        if ( !cellElement.isNull() )
                            values[cellElement.tagName()] = cellElement.text();

                        currentCell = currentCell.nextSibling();

                    }

                    data.append(values);

                }

                currentRow = currentRow.nextSibling();

            }

            setData(data);

        } else {

            qWarning( "QpwXML: Can't read %sfile", qPrintable( file.fileName() ) );
            ok = false;

        }

        file.close();

    } else {

        qWarning( "QpwXML: Can't open %s file for load", qPrintable(filePath) );
        ok = false;

    }

    return ok;
}

bool Database::save( const QString & filePath ) {

    QFile file( filePath.isEmpty() ? p_FilePath : filePath );
    bool ok = true;
    p_FilePath = filePath;

    if ( file.open(QIODevice::WriteOnly) ) {

        QDomDocument domDocument;
        QDomElement rootElement = domDocument.createElement("rows");

        foreach ( Values values, data() ) {

            QDomElement rowElement = domDocument.createElement("row");
            QMapIterator<QString,QVariant> iter(values);

            while ( iter.hasNext() ) {

                iter.next();

                QDomElement cellElement = domDocument.createElement( iter.key() );
                QDomText cellTextElement = domDocument.createTextNode( iter.value().toString() );
                cellElement.appendChild(cellTextElement);
                rowElement.appendChild(cellElement);

            }

            rootElement.appendChild(rowElement);

        }
        domDocument.appendChild(rootElement);

        file.write( domDocument.toString(2).toAscii() );
        file.close();

    } else {

        qWarning( "QpwXML: Can't open %s file for save", qPrintable(filePath) );
        ok = false;

    }

    return ok;
}

/**************************************************************************************************/
